package com.services;

import javax.swing.JTextArea;

import com.conf.MenuEnum;
import com.log.LogMonitorFactroy;
import com.msg.ShowMessage;
import com.msg.impl.Log2TextArea;
import com.utils.DateUtils;
import com.utils.ShellUtil;

public class GenCES {
	private ShellUtil sh1;
	private ShellUtil sh2;
	private String name;
	private static String STG1_ORACLE_IP = "10.194.20.24";
	private static String STG1_ORACLE_USER = "oracle";
	private static String STG1_ORACLE_PWD = "oracle";
	private static String STG1_APP_IP = "10.194.17.128";
	private static String STG1_APP_USER = "weblogic";
	private static String STG1_APP_PWD = "weblogic";
	private static String STG3_ORACLE_IP = "10.194.20.184";
	private static String STG3_ORACLE_USER = "oracle";
	private static String STG3_ORACLE_PWD = "oracle";
	private static String STG3_APP_IP = "10.194.10.134";
	private static String STG3_APP_USER = "weblogic";
	private static String STG3_APP_PWD = "weblogic";

	private static ShellUtil STG1_ORACLE = new ShellUtil(STG1_ORACLE_IP,
			STG1_ORACLE_USER, STG1_ORACLE_PWD);

	private static ShellUtil STG1_APP = new ShellUtil(STG1_APP_IP,
			STG1_APP_USER, STG1_APP_PWD);

	private static ShellUtil STG3_ORACLE = new ShellUtil(STG3_ORACLE_IP,
			STG3_ORACLE_USER, STG3_ORACLE_PWD);

	private static ShellUtil STG3_APP = new ShellUtil(STG3_APP_IP,
			STG3_APP_USER, STG3_APP_PWD);
	
	public GenCES(String name) throws Exception {
		this.name = name;
		if ("STG1".equals(name)) {
			sh1 = getSTG1_ORACLE();
			sh2 = getSTG1_APP();
		} else if ("STG3".equals(name)) {
			sh1 = getSTG3_ORACLE();
			sh2 = getSTG3_APP();
		} else {
			throw new Exception("错误的环境信息");
		}
	}

	public void generalStep(MenuEnum menu) {
		try {
			if (menu.equals(MenuEnum.all_batch)) {
				general(sh1, sh2);
			} else {
				if ("app".equals(menu.machine)) {
					sh2.exec(menu);
				} else if ("oracle".equals(menu.machine)) {
					sh1.exec(menu);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean general(ShellUtil sh1, ShellUtil sh2) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("日期：").append(DateUtils.formatDate(DateUtils.yyyyMMdd)).append("\n");
		sb.append("环境名：").append(name).append("\n");
		sb.append("系统名：对公评级系统_CES").append("\n");
		sb.append("批前日期：")
				.append(DateUtils.dateformat(CESDate.getSysDate(name),
						"yyyymmdd")).append("\n");
		sb.append("实际启动时间：").append(DateUtils.formatTime()).append("\n");
		int status = -1;
		status = sh1.exec(MenuEnum.oracle_batch);
		if (status == 0) {
			status = sh2.exec(MenuEnum.app_batch);
			if (status == 0) {
				status = sh1.exec(MenuEnum.oracle_date);
			}
		}
		if (status == 0) {
			sb.append("批后日期：").append(DateUtils.dateformat(CESDate.getSysDate(name),
					"yyyymmdd")).append("\n");
			sb.append("实际完成时间：")
					.append(DateUtils.formatTime()).append("\n");
			sb.append("完成状态：执行成功").append("\n");
		}
		LogMonitorFactroy.newInstance().append(sb.toString());
		return status == 0;
	}

	private ShellUtil getSTG1_ORACLE() {
		return STG1_ORACLE;
	}

	private ShellUtil getSTG1_APP() {
		return STG1_APP;
	}

	private ShellUtil getSTG3_ORACLE() {
		return STG3_ORACLE;
	}

	private ShellUtil getSTG3_APP() {
		return STG3_APP;
	}

	public void test(JTextArea log) {
		ShellUtil sh = new ShellUtil("", "", "");
		ShowMessage msg = new Log2TextArea(log);
		try {
			sh.exec(new String[] { "D:\\test.bat" }, msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}